package com.hucheng.rest;

import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hucheng.common.constant.UserConstant;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.request.ServiceCoreQuery;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.common.utils.ContextUtils;
import com.hucheng.common.utils.RedisUtils;
import com.hucheng.rest.base.BaseAction;
import com.hucheng.rest.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @Author: 凉白开不加冰
 * @Date: 2019-06-17 14:29
 * @Description: 项目工程请求入口
 **/
@RestController
@SpringBootApplication
@EnableDubboConfiguration
public class DubboRestApplication extends BaseAction implements CommandLineRunner {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(DubboRestApplication.class, args);
        ContextUtils.setApplicationContext(applicationContext);
    }

    /**
     * 请求入口
     * @param serviceCoreQuery 请求参数
     * @param request
     * @return
     * @throws EduException
     */
    @PostMapping("/")
    public ServiceCoreVO doService(@RequestBody ServiceCoreQuery serviceCoreQuery, HttpServletRequest request) throws EduException {

        serviceCoreQuery.setAction(request.getHeader("action"));
        serviceCoreQuery.setUserId(Integer.parseInt(request.getHeader("userId")));
        serviceCoreQuery.setFormat(request.getHeader("format"));
        serviceCoreQuery.setAccessId(request.getHeader("accessId"));
        serviceCoreQuery.setSign(request.getHeader("sign"));

        return new BaseService().response(serviceCoreQuery, request, stringRedisTemplate);
    }

    /**
     * 项目启动初始化
     * @param strings
     * @throws Exception
     */
    @Override
    public void run(String... strings) throws Exception {
        //初始化accessId
//        RedisUtils.set(stringRedisTemplate, UserConstant.ACCESS_KEY, UUID.randomUUID().toString().replaceAll("-", ""));

        //初始化限流信息
        final String remoteAddress = "127.0.0.1:2181";
        final String path = "/dubbo-rest/rule";

        ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>
                (remoteAddress, path, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
    }
}
